package com.newblog.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import com.newblog.model.Option;

public class OptionDAOMysql implements OptionDAO {
	public List listOptions(int bid) {
		List list = new LinkedList();
		PreparedStatement pstmt = null;
		ResultSet rs            = null;
		Option option = null;
		try{
			pstmt = conn.prepareStatement(LISTOPTIONS);
			pstmt.setInt(1, bid);
			rs = pstmt.executeQuery();
			while(rs.next()){
				option = new Option();
				option.setName(rs.getString("option_name"));
				option.setValue(rs.getString("option_value"));
				list.add(option);
				option = null;
			}
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		return list;
		
	}

	private static final Logger logger = Logger.getLogger(OptionDAOMysql.class);
	private static final String ADDOPTION="INSERT INTO nb_options(blog_id,option_name,option_value) VALUES(?,?,?)";
	private static final String UPDATEOPTION="UPDATE nb_options SET option_value=? WHERE option_name=? AND blog_id=?";
	private static final String LISTOPTIONS="SELECT * nb_options WHERE option_bid = ?";
	private Connection conn;
	private BlogFactory factory;
	
	public OptionDAOMysql(Connection conn)
	{
		this.conn = conn;
	}
	
	public OptionDAOMysql(BlogFactory factory)
	{
		this.factory = factory;
	}
	
	public boolean addOption(Option option) {
		PreparedStatement pstmt = null;
		boolean isSuccess = false;
		try{
			pstmt = conn.prepareStatement(ADDOPTION);
			pstmt.setInt(1, option.getBid());
			pstmt.setString(2,option.getName());
			pstmt.setString(3, option.getValue());
			isSuccess = pstmt.executeUpdate()>0?true:false;
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return isSuccess;
	}
	public boolean sequenceInsert(Option[] options) {
		StringBuffer sql = new StringBuffer();
		String s = null;
		Option one = null;
		for(int i=0;i<options.length;i++){
			one = options[i];
		    sql.append("('"+one.getBid()+"','"+one.getName()+"','"+one.getValue()+"'),");
		}
		int lastIdx =  sql.lastIndexOf(",");
		s = sql.substring(0, lastIdx);
		sql =new StringBuffer("INSERT INTO nb_options(option_bid,option_name,option_value) VALUES ");
		sql.append(s);
		PreparedStatement pstmt = null;
		boolean isSuccess = false;
		try{
			pstmt = conn.prepareStatement(sql.toString());
			isSuccess = pstmt.executeUpdate()>0?true:false;
		}
		catch(SQLException e){
			e.printStackTrace();
		}
		
		System.out.println(sql);
		return isSuccess;
	}
	
	public boolean updateOption(Option option) {
	  PreparedStatement pstmt = null;
	  boolean isSuccess = false;
	  try{
		  pstmt = conn.prepareStatement(UPDATEOPTION);
		  pstmt.setString(1, option.getValue());
		  pstmt.setString(2, option.getName());
		  pstmt.setInt(3, option.getBid());
		  isSuccess = pstmt.executeUpdate()>0?true:false;
	  }
	  catch(SQLException e){
		  e.printStackTrace();
	  }
		return isSuccess;
	}
	
	public static void main(String[] args){
/*		OptionDAOMysql ddd = new OptionDAOMysql(null);
		Option[] options = {new Option("A","AA"),new Option("B","BB"),new Option("c","cc")};
		ddd.sequenceInsert(options);*/
	}



}
